#include <QtGui>
#include <QLabel>

#include "mainwindow.h"
#include "mapItem.h"
#include "mapScene.h"

#include "appConfig.h"

const int InsertTextButton = 10;

MainWindow::MainWindow()
{
    createActions();
    createToolBox();
    createMenus();

    scene = new MapScene(itemMenu, this);
    scene->setSceneRect(QRectF(0, 0, 4000, 4000));
    connect(scene, SIGNAL(startInserted(int)), this, SLOT(startInserted(int)));
    connect(scene, SIGNAL(itemInserted(MapItem*)), this, SLOT(itemInserted(MapItem*)));
    createToolbars();

    QHBoxLayout *layout = new QHBoxLayout;
    
    view = new QGraphicsView(scene);

	view->setMouseTracking(true);
    layout->addWidget(view);
	layout->addWidget(toolBox);

    QWidget *widget = new QWidget;
    widget->setLayout(layout);

    setCentralWidget(widget);
	setWindowTitle(tr(AppConfig::windowName));
    setUnifiedTitleAndToolBarOnMac(true);
	scene->setBackgroundBrush(QPixmap(AppConfig::backgroundImage));

    scene->update();
    view->update();
}

void MainWindow::createToolbars()
{
	filesToolBar = addToolBar(tr("Files"));
	filesToolBar->addAction(newFileAction);
	filesToolBar->addAction(saveFileAction);
	filesToolBar->addAction(openFileAction);

    QToolButton *pointerButton = new QToolButton;
    pointerButton->setCheckable(true);
    pointerButton->setChecked(true);
    pointerButton->setIcon(QIcon(":/images/pointer.png"));

	fileButtonGroup = new QButtonGroup(this);

	QToolButton *openFileButton = new QToolButton;
	openFileButton->setIcon(QIcon(":/images/open.png"));

	connect(fileButtonGroup, SIGNAL(buttonClicked(int)), this, SLOT(fileGroupClicked(int)));

    pointerTypeGroup = new QButtonGroup(this);
    pointerTypeGroup->addButton(pointerButton, int(MapScene::MoveItem));
    connect(pointerTypeGroup, SIGNAL(buttonClicked(int)), this, SLOT(pointerGroupClicked(int)));

    sceneScaleCombo = new QComboBox;

    QStringList scales;
    scales << tr("60%") << tr("75%") << tr("100%") << tr("125%") << tr("150%");
    sceneScaleCombo->addItems(scales);
    sceneScaleCombo->setCurrentIndex(2);

    connect(sceneScaleCombo, SIGNAL(currentIndexChanged(QString)),this, SLOT(sceneScaleChanged(QString)));

    pointerToolbar = addToolBar(tr("Pointer type"));
    pointerToolbar->addWidget(pointerButton);
    pointerToolbar->addWidget(sceneScaleCombo);
}

void writeBorders(QXmlStreamWriter &xmlWriter, MapItem *item)
{
	xmlWriter.writeStartElement("borders");
	xmlWriter.writeAttribute("tileId", QString::number(item->myDiagramType));
	switch(item->myDiagramType)
	{
		case MapItem::RoadSolidVertical:
			xmlWriter.writeTextElement("up", "none");
			xmlWriter.writeTextElement("down", "none");
			xmlWriter.writeTextElement("left", "solid");
			xmlWriter.writeTextElement("right", "solid");
            break;
		case MapItem::RoadSolidHorizontal:
			xmlWriter.writeTextElement("up", "solid");
			xmlWriter.writeTextElement("down", "solid");
			xmlWriter.writeTextElement("left", "none");
			xmlWriter.writeTextElement("right", "none");
            break;
		case MapItem::RoadEmpty:
   			xmlWriter.writeTextElement("up", "none");
			xmlWriter.writeTextElement("down", "none");
			xmlWriter.writeTextElement("left", "none");
			xmlWriter.writeTextElement("right", "none");
            break;
		case MapItem::CornerDL:
			xmlWriter.writeTextElement("up", "solid");
			xmlWriter.writeTextElement("down", "none");
			xmlWriter.writeTextElement("left", "none");
			xmlWriter.writeTextElement("right", "solid");
			break;
		case MapItem::CornerDR:
			xmlWriter.writeTextElement("up", "solid");
			xmlWriter.writeTextElement("down", "none");
			xmlWriter.writeTextElement("left", "solid");
			xmlWriter.writeTextElement("right", "none");
			break;
		case MapItem::CornerUL:
			xmlWriter.writeTextElement("up", "none");
			xmlWriter.writeTextElement("down", "solid");
			xmlWriter.writeTextElement("left", "none");
			xmlWriter.writeTextElement("right", "solid");
			break;
		case MapItem::CornerUR:
			xmlWriter.writeTextElement("up", "none");
			xmlWriter.writeTextElement("down", "solid");
			xmlWriter.writeTextElement("left", "solid");
			xmlWriter.writeTextElement("right", "none");
			break;
		case MapItem::DashU:
			xmlWriter.writeTextElement("up", "dashed");
			xmlWriter.writeTextElement("down", "none");
			xmlWriter.writeTextElement("left", "none");
			xmlWriter.writeTextElement("right", "none");
			break;
		case MapItem::DashD:
			xmlWriter.writeTextElement("up", "none");
			xmlWriter.writeTextElement("down", "dashed");
			xmlWriter.writeTextElement("left", "none");
			xmlWriter.writeTextElement("right", "none");
			break;
		case MapItem::DashR:
			xmlWriter.writeTextElement("up", "none");
			xmlWriter.writeTextElement("down", "none");
			xmlWriter.writeTextElement("left", "none");
			xmlWriter.writeTextElement("right", "dashed");
			break;
		case MapItem::DashL:
			xmlWriter.writeTextElement("up", "none");
			xmlWriter.writeTextElement("down", "none");
			xmlWriter.writeTextElement("left", "dashed");
			xmlWriter.writeTextElement("right", "none");
			break;
		case MapItem::SolidU:
    		xmlWriter.writeTextElement("up", "solid");
			xmlWriter.writeTextElement("down", "none");
			xmlWriter.writeTextElement("left", "none");
			xmlWriter.writeTextElement("right", "none");
			break;
		case MapItem::SolidD:
			xmlWriter.writeTextElement("up", "none");
			xmlWriter.writeTextElement("down", "solid");
			xmlWriter.writeTextElement("left", "none");
			xmlWriter.writeTextElement("right", "none");
			break;
		case MapItem::SolidR:
 			xmlWriter.writeTextElement("up", "none");
			xmlWriter.writeTextElement("down", "none");
			xmlWriter.writeTextElement("left", "none");
			xmlWriter.writeTextElement("right", "solid");
			break;
		case MapItem::SolidL:
			xmlWriter.writeTextElement("up", "none");
			xmlWriter.writeTextElement("down", "none");
			xmlWriter.writeTextElement("left", "solid");
			xmlWriter.writeTextElement("right", "none");
			break;
		case MapItem::IntersectionR:
			xmlWriter.writeTextElement("up", "solid");
			xmlWriter.writeTextElement("down", "solid");
			xmlWriter.writeTextElement("left", "none");
			xmlWriter.writeTextElement("right", "none");
			break;
		case MapItem::IntersectionL:
			xmlWriter.writeTextElement("up", "solid");
			xmlWriter.writeTextElement("down", "solid");
			xmlWriter.writeTextElement("left", "none");
			xmlWriter.writeTextElement("right", "none");
			break;
		case MapItem::IntersectionU:
			xmlWriter.writeTextElement("up", "none");
			xmlWriter.writeTextElement("down", "none");
			xmlWriter.writeTextElement("left", "solid");
			xmlWriter.writeTextElement("right", "solid");
			break;
		case MapItem::IntersectionD:
			xmlWriter.writeTextElement("up", "none");
			xmlWriter.writeTextElement("down", "none");
			xmlWriter.writeTextElement("left", "solid");
			xmlWriter.writeTextElement("right", "solid");
			break;
		case MapItem::Start:
			xmlWriter.writeTextElement("up", "none");
			xmlWriter.writeTextElement("down", "none");
			xmlWriter.writeTextElement("left", "none");
			xmlWriter.writeTextElement("right", "none");
			break;
		case MapItem::End:
			xmlWriter.writeTextElement("up", "none");
			xmlWriter.writeTextElement("down", "none");
			xmlWriter.writeTextElement("left", "none");
			xmlWriter.writeTextElement("right", "none");
			break;
	}
	xmlWriter.writeEndElement();
}

void MainWindow::saveFile()
{
	QString filename = QFileDialog::getSaveFileName(this, tr("Save file"),  QDir::currentPath(), tr("Map File (*.xml);;All files (*.*)") );
	if( !filename.isNull() )
	{
		QFile file(filename);
		if(file.open(QIODevice::WriteOnly))
		{
			QXmlStreamWriter xmlWriter(&file);
			xmlWriter.setAutoFormatting(true);
			xmlWriter.writeStartDocument();

			xmlWriter.writeStartElement("map");
				xmlWriter.writeStartElement("mapinfo");
						xmlWriter.writeEmptyElement("size");
							xmlWriter.writeAttribute("x", "100");
							xmlWriter.writeAttribute("y",  "100");
						
						xmlWriter.writeStartElement("entries");
							foreach(QGraphicsItem* item, scene->items())
							{
								MapItem* mapItem = (MapItem*)item;

								if(mapItem->diagramType() == MapItem::DiagramType::Start)
								{
									xmlWriter.writeEmptyElement("entry");
										xmlWriter.writeAttribute("x", QString::number((int)(mapItem->pos().x()/AppConfig::cellWidth), 10));
										xmlWriter.writeAttribute("y", QString::number((int)(mapItem->pos().y()/AppConfig::cellWidth), 10));
										xmlWriter.writeAttribute("id", QString::number(mapItem->entryId));
										xmlWriter.writeAttribute("value", QString::number(1.0f/scene->startsCounter));
								}
							}
						xmlWriter.writeEndElement();

						xmlWriter.writeStartElement("paths");
						
						for(int i = 0; i < pathsListView->count(); i++)
						{
							xmlWriter.writeStartElement("path");
							xmlWriter.writeAttribute("id", pathsListView->item(i)->text());

							int pathId = pathsListView->item(i)->text().toInt();

							foreach(QGraphicsItem* item, scene->items())
							{
								MapItem* mapItem = (MapItem*)item;

								if(mapItem->zValue() == 10+pathId)
								{
									xmlWriter.writeEmptyElement("position");
										xmlWriter.writeAttribute("x", QString::number((int)(mapItem->pos().x()/AppConfig::cellWidth), 10));
										xmlWriter.writeAttribute("y", QString::number((int)(mapItem->pos().y()/AppConfig::cellWidth), 10));
								}
							}

							xmlWriter.writeEndElement();
						}

						xmlWriter.writeEndElement();
					xmlWriter.writeEndElement();
					xmlWriter.writeStartElement("cells");
					foreach(QGraphicsItem *item, scene->items())
					{
						if(item->zValue() < 2)
						{
							xmlWriter.writeStartElement("cell");
								xmlWriter.writeAttribute("type", "road");
							//xmlWriter.writeEndElement();
	
							xmlWriter.writeEmptyElement("position");
								xmlWriter.writeAttribute("x", QString::number((int)(item->pos().x()/AppConfig::cellWidth), 10));
								xmlWriter.writeAttribute("y", QString::number((int)(item->pos().y()/AppConfig::cellWidth), 10));
							//xmlWriter.writeEndElement();
								
							xmlWriter.writeEmptyElement("entry");
							if(((MapItem*)item)->diagramType() == MapItem::DiagramType::Start)
							{
								xmlWriter.writeAttribute("value", "true");
							}
							else
							{
								xmlWriter.writeAttribute("value", "false");
							}
							xmlWriter.writeEmptyElement("exit");
							if(((MapItem*)item)->diagramType() == MapItem::DiagramType::End)
							{
								xmlWriter.writeAttribute("value", "true");
							}
							else
							{
								xmlWriter.writeAttribute("value", "false");
							}

							xmlWriter.writeEmptyElement("speedlimit");
								xmlWriter.writeAttribute("value", "3");
							//xmlWriter.writeEndElement();

							writeBorders(xmlWriter, (MapItem*)item);
							xmlWriter.writeEndElement();
						}
					}
				xmlWriter.writeEndElement();
			xmlWriter.writeEndElement();
		}

		
	}
}

void MainWindow::openFile()
{
	QString filename = QFileDialog::getOpenFileName(this, tr("Open Document"),  QDir::currentPath(), tr("Map File (*.xml);;All files (*.*)") );
	if( !filename.isNull() )
	{
		scene->clearAll();
		QFile file(filename);
		if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
			QMessageBox::critical(this, "XML File error",
								  "Couldn't open example.xml",
								  QMessageBox::Ok);
			return;
		}

		QXmlStreamReader xml(&file);
		while(!xml.atEnd() && !xml.hasError()) 
		{
			if(xml.tokenType() == QXmlStreamReader::StartElement)
			{
				if(xml.name() == "mapinfo") 
				{
					this->handleMapInfo(xml);
				}

				if(xml.name() == "cells") 
				{
					this->handleCells(xml);
				}
			}
			xml.readNext();
		}
		
		if(xml.hasError()) 
		{
			QMessageBox::critical(this,
								  "QXSRExample::parseXML",
								  xml.errorString(),
								  QMessageBox::Ok);
		}
		
		xml.clear();
	}
}

void MainWindow::handleMapInfo(QXmlStreamReader& xml)
{
	while(!(xml.tokenType() == QXmlStreamReader::EndElement && xml.name() == "mapinfo") || 			xml.hasError()) 
	{
        if(xml.tokenType() == QXmlStreamReader::StartElement && xml.name() == "path") 
		{
				QXmlStreamAttributes attributes = xml.attributes();
				int id = attributes.value("id").toString().toInt();
				
				pathsListView->addItem(attributes.value("id").toString());

				QPoint position;

				while(!(xml.tokenType() == QXmlStreamReader::EndElement && xml.name() == "path")) 
				{
					if(xml.tokenType() == QXmlStreamReader::StartElement && xml.name() == "position") 
					{
						QXmlStreamAttributes positionAttributes = xml.attributes();
						position.setX(positionAttributes.value("x").toString().toInt());
						position.setY(positionAttributes.value("y").toString().toInt());

						scene->addPathCell(position, id);
					}
					xml.readNext();
				}
         
        }
		xml.readNext();
    }
}

void MainWindow::handleCells(QXmlStreamReader& xml)
{
	xml.readNext();
    while(!(xml.tokenType() == QXmlStreamReader::EndElement)) 
	{
        if(xml.tokenType() == QXmlStreamReader::StartElement) 
		{
            if(xml.name() == "cell") 
			{
				xml.readNext();

				QPoint position;
				
				while(!(xml.tokenType() == QXmlStreamReader::EndElement && xml.name() == "cell") )
				{
					if(xml.name() == "position" && (xml.tokenType() == QXmlStreamReader::StartElement ))
					{
						QXmlStreamAttributes positionAttributes = xml.attributes();
						position.setX(positionAttributes.value("x").toString().toInt());
						position.setY(positionAttributes.value("y").toString().toInt());
					}
					if(xml.name() == "borders")
					{
						QXmlStreamAttributes borderAttribute = xml.attributes();
						scene->addSpecificTile(position, (MapItem::DiagramType)borderAttribute.value("tileId").toString().toInt());
					}
					xml.readNext();
				}
            }
        }
        xml.readNext();
    }
}

void MainWindow::createNewFile()
{
	switch( QMessageBox::information( this, "Warning",
                                      "Do you want to discard current progress?",
                                      "Ok", "Cancel")) 
	{
		case 0:
			scene->clearAll();	
			pathsListView->clear();
			break;
		case 1:
		default: 
			break;
    }
}

void MainWindow::fileGroupClicked(int id)
{

}

void MainWindow::buttonGroupClicked(int id)
{
    QList<QAbstractButton *> buttons = buttonGroup->buttons();
    foreach (QAbstractButton *button, buttons) 
	{
		if (buttonGroup->button(id) != button)
		{
			button->setChecked(false);
		}
    }
	if(!buttonGroup->button(id)->isChecked())
	{
		scene->setMode(MapScene::MoveItem);
	}
	else
	{
		if (id == InsertTextButton) {
			scene->setMode(MapScene::InsertText);
		} else {
			scene->setItemType(MapItem::DiagramType(id));
			scene->setMode(MapScene::InsertItem);
		}
	}
}

void MainWindow::deleteItem()
{
    foreach (QGraphicsItem *item, scene->selectedItems()) {
         scene->removeItem(item);
         delete item;
     }
}

void MainWindow::pointerGroupClicked(int)
{
    scene->setMode(MapScene::Mode(pointerTypeGroup->checkedId()));
}


void MainWindow::itemInserted(MapItem *item)
{
    //pointerTypeGroup->button(int(MapScene::MoveItem))->setChecked(true);
    //scene->setMode(MapScene::Mode(pointerTypeGroup->checkedId()));
    //buttonGroup->button(int(item->diagramType()))->setChecked(false);
}

void MainWindow::sceneScaleChanged(const QString &scale)
{
    double newScale = scale.left(scale.indexOf(tr("%"))).toDouble() / 100.0;
    QMatrix oldMatrix = view->matrix();
    view->resetMatrix();
    view->translate(oldMatrix.dx(), oldMatrix.dy());
    view->scale(newScale, newScale);
}

void MainWindow::about()
{
    QMessageBox::about(this, tr("About Map Editor"),
                       tr("Marcin Gwizdala Map Editor v.0.9"));
}

void MainWindow::createToolBox()
{
    buttonGroup = new QButtonGroup(this);
    buttonGroup->setExclusive(false);
    connect(buttonGroup, SIGNAL(buttonClicked(int)), this, SLOT(buttonGroupClicked(int)));
    
	QGridLayout *layout = new QGridLayout;
	layout->addWidget(createCellWidget(tr("Road solid vertical"), MapItem::RoadSolidVertical), 0, 0);
	layout->addWidget(createCellWidget(tr("Road solid horizontal"), MapItem::RoadSolidHorizontal), 0, 1);
	layout->addWidget(createCellWidget(tr("Road solid empty"), MapItem::RoadEmpty), 1, 0);

	QGridLayout *layout_2 = new QGridLayout;
	layout_2->addWidget(createCellWidget(tr("Corner Up Left"), MapItem::CornerUL), 0, 0);
	layout_2->addWidget(createCellWidget(tr("Corner Up Right"), MapItem::CornerUR), 0, 1);
	layout_2->addWidget(createCellWidget(tr("Corner Down Left"), MapItem::CornerDL), 1, 0);
	layout_2->addWidget(createCellWidget(tr("Corner Down Right"), MapItem::CornerDR), 1, 1);

	QGridLayout *layout_3 = new QGridLayout;
	layout_3->addWidget(createCellWidget(tr("Dash left"), MapItem::DashL), 0, 0);
	layout_3->addWidget(createCellWidget(tr("Dash s"), MapItem::DashR), 0, 1);
	layout_3->addWidget(createCellWidget(tr("Dash up"), MapItem::DashU), 1, 0);
	layout_3->addWidget(createCellWidget(tr("Dash down"), MapItem::DashD), 1, 1);

	QGridLayout *layout_4 = new QGridLayout;
	layout_4->addWidget(createCellWidget(tr("Solid left"), MapItem::SolidL), 0, 0);
	layout_4->addWidget(createCellWidget(tr("Solid right"), MapItem::SolidR), 0, 1);
	layout_4->addWidget(createCellWidget(tr("Solid down"), MapItem::SolidD), 1, 0);
	layout_4->addWidget(createCellWidget(tr("Solid up"), MapItem::SolidU), 1, 1);

	QGridLayout *layout_5 = new QGridLayout;
	layout_5->addWidget(createCellWidget(tr("Solid left"), MapItem::IntersectionL), 0, 0);
	layout_5->addWidget(createCellWidget(tr("Solid right"), MapItem::IntersectionR), 0, 1);
	layout_5->addWidget(createCellWidget(tr("Solid down"), MapItem::IntersectionU), 1, 0);
	layout_5->addWidget(createCellWidget(tr("Solid up"), MapItem::IntersectionD), 1, 1);
	
	QGridLayout *layout_6 = new QGridLayout;
	layout_6->addWidget(createCellWidget(tr("Start"), MapItem::Start), 0, 0);
	layout_6->addWidget(createCellWidget(tr("Exit"), MapItem::End), 0, 1);


	pathsListView = new QListWidget;
	
	connect(pathsListView, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(listItemClicked(QListWidgetItem*)));

	QBoxLayout *layout_7 = new QBoxLayout(QBoxLayout::Direction::TopToBottom);
	layout_7->addWidget(pathsListView);
	
	layout->setRowStretch(2, 10);
	layout_2->setRowStretch(2, 10);
	layout_3->setRowStretch(2, 10);
	layout_4->setRowStretch(2, 10);
	layout_5->setRowStretch(2, 10);
	layout_6->setRowStretch(2, 10);

	

    QWidget *itemWidget = new QWidget;
    itemWidget->setLayout(layout);

    QWidget *itemSWidget = new QWidget;
    itemSWidget->setLayout(layout_2);

	QWidget *itemDashes = new QWidget;
	itemDashes->setLayout(layout_3);
	
	QWidget *itemDashesx = new QWidget;
	itemDashesx->setLayout(layout_4);

	QWidget *itemIntersection = new QWidget;
	itemIntersection->setLayout(layout_5);

	QWidget *itemSpecial = new QWidget;
	itemSpecial->setLayout(layout_6);

	QWidget *paths = new QWidget;
	paths->setLayout(layout_7);

    toolBox = new QToolBox;
    toolBox->setSizePolicy(QSizePolicy(QSizePolicy::Maximum, QSizePolicy::Ignored));
    toolBox->setMinimumWidth(itemWidget->sizeHint().width());
    toolBox->addItem(itemWidget, tr("Road"));
    toolBox->addItem(itemSWidget, tr("Corner"));
    toolBox->addItem(itemDashes, tr("Dashed"));
    toolBox->addItem(itemDashesx, tr("Solid"));
	toolBox->addItem(itemIntersection, tr("Intersection"));
	toolBox->addItem(itemSpecial, tr("Special"));
	toolBox->addItem(paths, tr("Paths"));

	connect(toolBox, SIGNAL(currentChanged(int)), this, SLOT(toolboxSelectionChanged(int)));
}

void MainWindow::toolboxSelectionChanged(int boxId)
{
	pathsListView->clearSelection();
	scene->hideAllPaths();
}

void MainWindow::listItemClicked(QListWidgetItem* item)
{
	scene->hideAllPaths();
	if(buttonGroup->checkedButton() != NULL)
	{
		buttonGroup->checkedButton()->setChecked(false);
	}
	//scene->setMode(MapScene::Mode::InsertPath);
	scene->setItemType(MapItem::DiagramType::Path);
	scene->setPathGroupId(item->text().toInt());
	scene->showPathWithId(item->text().toInt());
}

void MainWindow::createActions()
{
	newFileAction = new QAction(QIcon(":/images/new_file.png"), tr("New file"), this);
	newFileAction->setShortcut(tr("Ctrl+N"));
	newFileAction->setStatusTip(tr("Create new empty file"));
	connect(newFileAction, SIGNAL(triggered()), this, SLOT(createNewFile()));

	openFileAction = new QAction(QIcon(":/images/open.png"), tr("Open file"), this);
	openFileAction->setShortcut(tr("Ctrl+O"));
	openFileAction->setStatusTip(tr("Open existing file"));
	connect(openFileAction, SIGNAL(triggered()), this, SLOT(openFile()));

	saveFileAction = new QAction(QIcon(":/images/save.png"), tr("Save file"), this);
	saveFileAction->setShortcut(tr("Ctrl+S"));
	saveFileAction->setStatusTip(tr("Save current file"));
	connect(saveFileAction, SIGNAL(triggered()), this, SLOT(saveFile()));

    deleteAction = new QAction(QIcon(":/images/delete.png"),
                               tr("&Delete"), this);
    deleteAction->setShortcut(tr("Delete"));
    deleteAction->setStatusTip(tr("Delete item from diagram"));
    connect(deleteAction, SIGNAL(triggered()),
        this, SLOT(deleteItem()));

    exitAction = new QAction(tr("E&xit"), this);
    exitAction->setShortcuts(QKeySequence::Quit);
    exitAction->setStatusTip(tr("Quits Map editor"));
    connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));

    aboutAction = new QAction(tr("A&bout"), this);
    connect(aboutAction, SIGNAL(triggered()),
            this, SLOT(about()));
}

void MainWindow::createMenus()
{
    fileMenu = menuBar()->addMenu(tr("&File"));
	fileMenu->addAction(newFileAction);
	fileMenu->addAction(openFileAction);
    fileMenu->addAction(exitAction);

	viewMenu = menuBar()->addMenu(tr("View"));
	viewMenu->addAction(deleteAction);

    aboutMenu = menuBar()->addMenu(tr("&Help"));
    aboutMenu->addAction(aboutAction);
}

QWidget *MainWindow::createBackgroundCellWidget(const QString &text,
                        const QString &image)
{
    QToolButton *button = new QToolButton;
    button->setText(text);
    button->setIcon(QIcon(image));
    button->setIconSize(QSize(50, 50));
    button->setCheckable(true);
    backgroundButtonGroup->addButton(button);

    QGridLayout *layout = new QGridLayout;
    layout->addWidget(button, 0, 0, Qt::AlignHCenter);
    layout->addWidget(new QLabel(text), 1, 0, Qt::AlignCenter);

    QWidget *widget = new QWidget;
    widget->setLayout(layout);

    return widget;
}

QWidget *MainWindow::createCellWidget(const QString &text,
                      MapItem::DiagramType type)
{

    MapItem item(type, itemMenu);
    QIcon icon(item.image());

    QToolButton *button = new QToolButton;
    button->setIcon(icon);
    button->setIconSize(QSize(50, 50));
    button->setCheckable(true);
    buttonGroup->addButton(button, int(type));

    QGridLayout *layout = new QGridLayout;
    layout->addWidget(button, 0, 0, Qt::AlignHCenter);
    layout->addWidget(new QLabel(text), 1, 0, Qt::AlignCenter);

    QWidget *widget = new QWidget;
    widget->setLayout(layout);

    return widget;
}

void MainWindow::startInserted(int id)
{
	pathsListView->addItem(QString::number(id));
}